#version 460 core

layout (location=0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;

uniform vec4 lightPos;
uniform float time;
uniform float prevTime;
uniform vec3 noiseScale;
uniform vec4 lightAmbient;
uniform vec4 lightDiffuse;              
uniform vec4 lightSpecular;  
uniform vec3 viewPos;
uniform vec4 materialAmbient;           
uniform vec4 materialDiffuse;           
uniform vec4 materialSpecular;          
uniform float materialShininess;
uniform sampler2D map0;


in vec3 FragPos;
in vec3 Normal;
in vec2 texCoord0;
in float baseradius;
in float scalar;
uniform vec3 center;


void main()
{

    float appRadius=baseradius*(scalar*.87333);
    float Difference=length(FragPos-center)-appRadius;

    vec3 deepBlue = vec3(0.0, 0.0, 0.4);    
    vec3 oceanBlue = vec3(0.0, 0.0, 0.9);   
    vec3 forestGreen = vec3(0.0, 0.6, 0.0); 
    vec3 dirtBrown=vec3(0.48, 0.3, 0.19);
    vec3 mountainGray = vec3(0.4, 0.4, 0.4);
    vec3 snowWhite = vec3(0.9, 0.9, 0.9);

    

    float material=materialShininess;


float deepOceanTransition = smoothstep(-0.06*appRadius, -0.04*appRadius, Difference);
float oceanTransition = smoothstep(-0.04*appRadius, -0.02*appRadius, Difference);
float landTransition = smoothstep(-0.02*appRadius, 0.01*appRadius, Difference);
float mountainTransition = smoothstep(0.01*appRadius, 0.03*appRadius, Difference);
float snowTransition = smoothstep(0.03*appRadius, 0.05*appRadius, Difference);

vec3 finalColor = deepBlue;  

finalColor = mix(finalColor, oceanBlue, deepOceanTransition);

finalColor = mix(finalColor, dirtBrown, oceanTransition);

finalColor = mix(finalColor, forestGreen, landTransition);

finalColor = mix(finalColor, mountainGray, mountainTransition);

finalColor = mix(finalColor, snowWhite, snowTransition);

    vec3 toCenter=normalize(vec3(0.0)-FragPos);
    vec3 sunsurface=baseradius*-toCenter;
    vec2 wrappedTexCoord=fract(texCoord0);
    vec3 normal = normalize(Normal);
    vec3 lightDir=normalize(-FragPos);
    vec3 view = normalize(viewPos - FragPos);
    vec3 reflectVec = reflect(-lightDir, normal);

    vec3 ambient = lightAmbient.rgb * materialAmbient.rgb;
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse= lightDiffuse.rgb * materialDiffuse.rgb * diff;
     float spec = pow(max(dot(view, reflectVec), 0.0), materialShininess);
     float fspec=0;
    if(Difference < -0.02*appRadius) {
    material = 16.0;
    float fresnel =pow(1.0-max(dot(normal,view),0.0),5.0);
    fspec=mix(1.0,1.0,fresnel);

    float waves=sin(texCoord0.x*0.0+time)*sin(texCoord0.y*50.0+time)*0.5+0.5;
    fspec*=(0.8+waves*0.2);
} 
else if(Difference >= -0.02*appRadius && Difference < 0.03*appRadius) {
    material = 32.0;
    fspec = 0.3;
} 
else {
    material = 64.0;
    fspec=0.8;
}



    

    spec = pow(max(dot(view, reflectVec), 0.0), material);
    vec3 finalspec=lightSpecular.rgb*materialSpecular.rgb*spec*fspec;
    float distance = length(FragPos);
    float attenuation = 1.0 / (1.0 + 0.09 * distance + 0.032 * distance * distance);
    diffuse *= attenuation;
    finalspec *= attenuation;
   
   vec3  color=(ambient+diffuse)*finalColor+finalspec;

    float rim = 1.0 - max(dot(normal, view), 0.0);
    rim = pow(rim, 10.0);
    color += rim * vec3(0.6, 0.7, 1.0);





 
    FragColor = vec4(color, 1.0);
    BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
}